package com.huakang.controller;


import com.huakang.entity.Account;
import com.huakang.service.AccountService;
import com.huakang.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    public AccountService accountService;

    @PostMapping ("/login")
    public ResultVo login(@RequestBody Account account, HttpSession session){
        Account loginAccount = accountService.login(account);
        if (loginAccount!=null){
            session.setAttribute("loginAccount",loginAccount);
            return ResultVo.success("登陆成功",loginAccount);
        }
        return ResultVo.fail("登录失败");
    }
    @PostMapping("/reg")
    public ResultVo reg(@RequestBody Account account){
        boolean reg = accountService.reg(account);
        if(reg){
            return ResultVo.success("注册成功");
        }return ResultVo.fail("注册失败");
    }

    @GetMapping("/logout")
    public ResultVo logout(HttpSession session){
        session.removeAttribute("loginAccount");
        return ResultVo.success("退出成功！");
    }

}
